﻿
namespace SPL.Windows.Forms
{
	/// <summary>
	/// abstract User typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class UserDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<UserDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.User _currentUser = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxUserDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxUserErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxUserBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the Id property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIdDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Username property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxUsernameDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the DeptId property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxDeptIdDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the RoleId property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxRoleIdDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the IsActive property
		/// </summary>
		protected System.Windows.Forms.DataGridViewCheckBoxColumn uxIsActiveDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Password property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxPasswordDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
				
		private Entities.TList<Entities.Role> _RoleIdList;
		
		/// <summary> 
		/// The list of selectable Role
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.Role> RoleIdList
		{
			get {return this._RoleIdList;}
			set 
			{
				this._RoleIdList = value;
				this.uxRoleIdDataGridViewColumn.DataSource = null;
				this.uxRoleIdDataGridViewColumn.DataSource = this._RoleIdList;
			}
		}
		
		private bool _allowNewItemInRoleIdList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of Role
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInRoleIdList
		{
			get { return _allowNewItemInRoleIdList;}
			set
			{
				this._allowNewItemInRoleIdList = value;
				this.uxRoleIdDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
				
		private Entities.TList<Entities.Department> _DeptIdList;
		
		/// <summary> 
		/// The list of selectable Department
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.Department> DeptIdList
		{
			get {return this._DeptIdList;}
			set 
			{
				this._DeptIdList = value;
				this.uxDeptIdDataGridViewColumn.DataSource = null;
				this.uxDeptIdDataGridViewColumn.DataSource = this._DeptIdList;
			}
		}
		
		private bool _allowNewItemInDeptIdList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of Department
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInDeptIdList
		{
			get { return _allowNewItemInDeptIdList;}
			set
			{
				this._allowNewItemInDeptIdList = value;
				this.uxDeptIdDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.User> _UserList;
				
		/// <summary> 
		/// The list of User to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.User> UserList
		{
			get {return this._UserList;}
			set
			{
				this._UserList = value;
				this.uxUserBindingSource.DataSource = null;
				this.uxUserBindingSource.DataSource = value;
				this.uxUserDataGridView.DataSource = null;
				this.uxUserDataGridView.DataSource = this.uxUserBindingSource;				
				//this.uxUserBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxUserBindingSource_ListChanged);
				this.uxUserBindingSource.CurrentItemChanged += new System.EventHandler(OnUserBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnUserBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentUser = uxUserBindingSource.Current as Entities.User;
			
			if (_currentUser != null)
			{
				_currentUser.Validate();
			}
			//_User.Validate();
			OnCurrentEntityChanged();
		}

		//void uxUserBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.User"/> instance.
		/// </summary>
		public Entities.User SelectedUser
		{
			get {return this._currentUser;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxUserDataGridView.VirtualMode;}
			set
			{
				this.uxUserDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxUserDataGridView.AllowUserToAddRows;}
			set {this.uxUserDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxUserDataGridView.AllowUserToDeleteRows;}
			set {this.uxUserDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxUserDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxUserDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="UserDataGridViewBase"/> class.
		/// </summary>
		public UserDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxUserDataGridView = new System.Windows.Forms.DataGridView();
			this.uxUserBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxUserErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIdDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxUsernameDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxDeptIdDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxRoleIdDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxIsActiveDataGridViewColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn();
			this.uxPasswordDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			//this.uxRoleIdBindingSource = new RoleBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxRoleIdBindingSource)).BeginInit();
			//this.uxDeptIdBindingSource = new DepartmentBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxDeptIdBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxUserDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxUserBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxUserErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxUserErrorProvider
			// 
			this.uxUserErrorProvider.ContainerControl = this;
			this.uxUserErrorProvider.DataSource = this.uxUserBindingSource;						
			// 
			// uxUserDataGridView
			// 
			this.uxUserDataGridView.AutoGenerateColumns = false;
			this.uxUserDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxUserDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIdDataGridViewColumn,
		this.uxUsernameDataGridViewColumn,
		this.uxDeptIdDataGridViewColumn,
		this.uxRoleIdDataGridViewColumn,
		this.uxIsActiveDataGridViewColumn,
		this.uxPasswordDataGridViewColumn			});
			this.uxUserDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxUserDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxUserDataGridView.Name = "uxUserDataGridView";
			this.uxUserDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxUserDataGridView.TabIndex = 0;	
			this.uxUserDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxUserDataGridView.EnableHeadersVisualStyles = false;
			this.uxUserDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnUserDataGridViewDataError);
			this.uxUserDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnUserDataGridViewCellValueNeeded);
			this.uxUserDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnUserDataGridViewCellValuePushed);
			
			//
			// uxIdDataGridViewColumn
			//
			this.uxIdDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIdDataGridViewColumn.DataPropertyName = "Id";
			this.uxIdDataGridViewColumn.HeaderText = "Id";
			this.uxIdDataGridViewColumn.Name = "uxIdDataGridViewColumn";
			this.uxIdDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIdDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIdDataGridViewColumn.ReadOnly = true;		
			//
			// uxUsernameDataGridViewColumn
			//
			this.uxUsernameDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxUsernameDataGridViewColumn.DataPropertyName = "Username";
			this.uxUsernameDataGridViewColumn.HeaderText = "Username";
			this.uxUsernameDataGridViewColumn.Name = "uxUsernameDataGridViewColumn";
			this.uxUsernameDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxUsernameDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxUsernameDataGridViewColumn.ReadOnly = false;		
			//
			// uxDeptIdDataGridViewColumn
			//
			this.uxDeptIdDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxDeptIdDataGridViewColumn.DataPropertyName = "DeptId";
			this.uxDeptIdDataGridViewColumn.HeaderText = "DeptId";
			this.uxDeptIdDataGridViewColumn.Name = "uxDeptIdDataGridViewColumn";
			this.uxDeptIdDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxDeptIdDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxDeptIdDataGridViewColumn.ReadOnly = false;		
			//
			// uxRoleIdDataGridViewColumn
			//
			this.uxRoleIdDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxRoleIdDataGridViewColumn.DataPropertyName = "RoleId";
			this.uxRoleIdDataGridViewColumn.HeaderText = "RoleId";
			this.uxRoleIdDataGridViewColumn.Name = "uxRoleIdDataGridViewColumn";
			this.uxRoleIdDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxRoleIdDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxRoleIdDataGridViewColumn.ReadOnly = false;		
			//
			// uxIsActiveDataGridViewColumn
			//
			this.uxIsActiveDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIsActiveDataGridViewColumn.DataPropertyName = "IsActive";
			this.uxIsActiveDataGridViewColumn.HeaderText = "IsActive";
			this.uxIsActiveDataGridViewColumn.Name = "uxIsActiveDataGridViewColumn";
			this.uxIsActiveDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIsActiveDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIsActiveDataGridViewColumn.ReadOnly = false;		
			//
			// uxPasswordDataGridViewColumn
			//
			this.uxPasswordDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxPasswordDataGridViewColumn.DataPropertyName = "Password";
			this.uxPasswordDataGridViewColumn.HeaderText = "Password";
			this.uxPasswordDataGridViewColumn.Name = "uxPasswordDataGridViewColumn";
			this.uxPasswordDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxPasswordDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxPasswordDataGridViewColumn.ReadOnly = false;		
			//
			// uxRoleIdDataGridViewColumn
			//				
			this.uxRoleIdDataGridViewColumn.DisplayMember = "Name";	
			this.uxRoleIdDataGridViewColumn.ValueMember = "Id";	
			this.uxRoleIdDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxRoleIdDataGridViewColumn.DataSource = uxRoleIdBindingSource;				
				
			//
			// uxDeptIdDataGridViewColumn
			//				
			this.uxDeptIdDataGridViewColumn.DisplayMember = "Name";	
			this.uxDeptIdDataGridViewColumn.ValueMember = "Id";	
			this.uxDeptIdDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxDeptIdDataGridViewColumn.DataSource = uxDeptIdBindingSource;				
				
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxUserDataGridView);
			this.Name = "UserDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			//((System.ComponentModel.ISupportInitialize)(this.uxRoleIdBindingSource)).EndInit();
			//((System.ComponentModel.ISupportInitialize)(this.uxDeptIdBindingSource)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxUserErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxUserDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxUserBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				UserDataGridViewEventArgs args = new UserDataGridViewEventArgs();
				args.User = _currentUser;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class UserDataGridViewEventArgs : System.EventArgs
		{
			private Entities.User	_User;
	
			/// <summary>
			/// the  Entities.User instance.
			/// </summary>
			public Entities.User User
			{
				get { return _User; }
				set { _User = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxUserDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnUserDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxUserDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnUserDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxUserDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIdDataGridViewColumn":
						e.Value = UserList[e.RowIndex].Id;
						break;
					case "uxUsernameDataGridViewColumn":
						e.Value = UserList[e.RowIndex].Username;
						break;
					case "uxDeptIdDataGridViewColumn":
						e.Value = UserList[e.RowIndex].DeptId;
						break;
					case "uxRoleIdDataGridViewColumn":
						e.Value = UserList[e.RowIndex].RoleId;
						break;
					case "uxIsActiveDataGridViewColumn":
						e.Value = UserList[e.RowIndex].IsActive;
						break;
					case "uxPasswordDataGridViewColumn":
						e.Value = UserList[e.RowIndex].Password;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxUserDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnUserDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxUserDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIdDataGridViewColumn":
						UserList[e.RowIndex].Id = (System.Int32)e.Value;
						break;
					case "uxUsernameDataGridViewColumn":
						UserList[e.RowIndex].Username = (System.String)e.Value;
						break;
					case "uxDeptIdDataGridViewColumn":
						UserList[e.RowIndex].DeptId = (System.Int32)e.Value;
						break;
					case "uxRoleIdDataGridViewColumn":
						UserList[e.RowIndex].RoleId = (System.Int32)e.Value;
						break;
					case "uxIsActiveDataGridViewColumn":
						UserList[e.RowIndex].IsActive = (System.Boolean)e.Value;
						break;
					case "uxPasswordDataGridViewColumn":
						UserList[e.RowIndex].Password = (System.String)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
